import Vue from 'vue';

import VueRouter from 'vue-router';


//使用路由懒加载引入组件
const Home = () => import("@pages/Home");
const Search = () => import("@pages/Search");
const Login = () => import("@pages/Login");
const Register = () => import("@pages/Register");

Vue.use(VueRouter);
//重新路由器push方法 
//1.先获取原始的push方法,供自定义push是使用
const lastPush = VueRouter.prototype.push;
/* 
  为什么要重写push,因为push方法返回promise实例,
  但是如果我们不传递第三个参数onAbort,则push内部的catch函数没有处理异常的回调函数,
  所以如果push出错,就会直接抛出错误到浏览器上
*/
//2.重写push,
//覆盖之前的push方法,
//但是我们的push方法默认传入了onComplate和onAbort参数的默认参数为一个函数,那么如果push出现问题后,就会有catch的函数帮我们处理异常了,这个就是重写push的核心
VueRouter.prototype.push=function(
location,
//固定写法
onComplete=()=>{},
onAbort=()=>{}
){ 
    return lastPush.call(this,location,onComplete, onAbort)
};

export default new VueRouter({
    mode: "history",
    routes: [
      {
        path: "/home",
        component: Home,
        name: "home",
      },
      {
        path: "/search",
        //      path: "/search/:id?", 测试path值是否改变
        component: Search,
        name: "search",
      },
      {
        path: "/login",
        component: Login,
        name: "login",
        meta:{footerIsHidden:true},
      },
      {
        path: "/register",
        component: Register,
        name: "register",
        meta:{footerIsHidden:true},
      },
      //默认加载
      {
        path: "/",
        redirect: "/home",
      },
    ],

  });